//
//  InsertModalViewController.m
//  cherryPicker
//
//  Created by Young Soo Yun on 12. 1. 2..
//  Copyright (c) 2012 Happy. All rights reserved.
//

#import "InsertModalViewController.h"


#import "ParsingDataMgr.h"
#import "ControlUtil.h"
#import "Options.h"

@implementation InsertModalViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    
    _tvSMS.text = pasteboard.string;
    
    if ([[Options getValue:OptionUser1 valueName:@"popup"] isEqualToString:@"1"]) {
        [self goParsing];
    }
    
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        _makeCardVC = [[MakeCardViewController alloc] initWithNibName:@"MakeCardViewController" bundle:nil];
        [_makeCardVC set_delegate:self];

        [_tvSMS resignFirstResponder];

        [self.view addSubview:_makeCardVC.view];
        
        NSArray* arrStr = [ParsingDataMgr delimiterList:_tvSMS.text];
        [_makeCardVC setLabel:[arrStr objectAtIndex:0] tag:TAG_DELIMITER];
    }
}

#pragma mark - MakeCardVCDelegate

- (void)MakeCardOKClick{
    [_makeCardVC.view removeFromSuperview];
}

- (void)MakeCardCancelClick{
    [_makeCardVC.view removeFromSuperview];
}

- (void)MakeCardPickerView:(NSString *)labText
                       tag:(NSInteger)tag{
    SimplePickerViewController * controller = [[SimplePickerViewController alloc] initWithNibName:@"SimplePickerViewController" bundle:nil];
    controller.delegate = self;
    
    _simplePickerTag = tag;

    switch (tag) {
        case TAG_DELIMITER:
            controller.dataArray = [ParsingDataMgr delimiterList:_tvSMS.text];
            break;
        case TAG_COMPANY:
            controller.dataArray = [[NSArray alloc] initWithObjects:@"현대카드",
                                    @"신한카드",
                                    @"KB국민카드",
                                    @"롯데카드",
                                    @"외환카드",
                                    @"시티은행카드(구 한미카드)",
                                    @"우리카드(평화)",
                                    @"비씨카드",
                                    @"삼성카드",
                                    @"하나SK카드",
                                    @"광주은행카드",
                                    @"수협은행카드",
                                    @"전북은행카드",
                                    @"제주은행카드",
                                    @"강원은행카드",
                                    @"저축은행체크카드",
                                    @"산업은행카드",
                                    @"농협NH카드",
                                    nil];
            break;
        case TAG_PAYDATE:
        case TAG_USEDATE:{
            NSMutableArray* arrDate = [[NSMutableArray alloc] init];
            for (int i = 1; i < 31; i++) {
                [arrDate addObject:[NSString stringWithFormat:@"%d 일", i]];
            }
            controller.dataArray = arrDate;
            
            break;
        }
        case TAG_PRICE:{
            NSMutableArray* arrMoney = [[NSMutableArray alloc] init];
            for (int i = 1; i < 11; i++) {
                [arrMoney addObject:[NSString stringWithFormat:@"%d 만원", i*10]];
            }
            controller.dataArray = arrMoney;
            break;
        }
    }
    
    for (int i = 0; i < controller.dataArray.count; i++) {
        if ([labText isEqualToString:[controller.dataArray objectAtIndex:i]]) {
            [controller setPickerIndex:i];
            break;
        }
    }
    
    [self.view addSubview:controller.view];
}

#pragma mark - ConfirmVCDelegate

- (void)ConfirmOKClick{
    
    [_confirmVC.view removeFromSuperview];
    [self dismissModalViewControllerAnimated:YES];
}

- (void)ConfirmCancelClick{
    [_confirmVC.view removeFromSuperview];
}

- (void)ConfirmPickerView:(NSInteger)tag labText:(NSString *)labText{
    switch (tag) {
        case TAG_CARDNAME:{
            SimplePickerViewController * controller = [[SimplePickerViewController alloc] initWithNibName:@"SimplePickerViewController" bundle:nil];
            controller.delegate = self;
            
            _simplePickerTag = tag;
            
            NSArray* arrCardList = [CherryPickerDB selectFromCardList];
            NSMutableArray* dataArray = [[NSMutableArray alloc] init];
            
            for (int i = 0; i < arrCardList.count; i++) {
                CardListDB* cardData = [arrCardList objectAtIndex:i];
                [dataArray addObject:cardData._CardName];
            }
            
            controller.dataArray = dataArray;
            
            [self.view addSubview:controller.view];
            
            break;
        }
        case TAG_DATE:{
            DatePickerViewController * controller = [[DatePickerViewController alloc] initWithNibName:@"DatePickerViewController" bundle:nil];
            controller.delegate = self;
            [controller initDatePosition:labText];
            _simplePickerTag = tag;
            
            [self.view addSubview:controller.view];
            
            break;
        }
        case TAG_CATEGORY:{
            SimplePickerViewController * controller = [[SimplePickerViewController alloc] initWithNibName:@"SimplePickerViewController" bundle:nil];
            controller.delegate = self;
            
            _simplePickerTag = tag;
            
            controller.dataArray = [[NSArray alloc] initWithObjects:@"식비",
                                    @"주거",
                                    @"통신",
                                    @"생활용품",
                                    @"의복",
                                    @"건강",
                                    @"문화"
                                    @"교통",
                                    @"교육",
                                    @"육아",
                                    nil];;
            [self.view addSubview:controller.view];
            
            break;
        }
        case TAG_PLACE:{
            SimplePickerViewController * controller = [[SimplePickerViewController alloc] initWithNibName:@"SimplePickerViewController" bundle:nil];
            controller.delegate = self;
            
            _simplePickerTag = tag;
            controller.dataArray = [ParsingDataMgr delimiterList:_tvSMS.text];
            
            [self.view addSubview:controller.view];
            break;

        }
    }
}

- (void)ConfimKeyBoard:(BOOL)isON{

    CGRect moveFrame = (isON)?CGRectMake(0.0f, - 100.0f, 320.0f, 460.0f):
    CGRectMake(0.0f, 0.0f, 320.0f, 460.0f);
    
    CGFloat animationDuration = 0.3;
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];
    _confirmVC.view.frame = moveFrame;
    [UIView commitAnimations];
}

#pragma mark - SimplePickerViewControllerDelegate

- (void)simplePickerViewControllerDidSelected:(SimplePickerViewController *)controller selectedIndex:(int)index{
    
    if (_simplePickerTag < 6) {
        [_makeCardVC setLabel:[controller.dataArray objectAtIndex:index] tag:_simplePickerTag];
    }else{
        [_confirmVC setLabel:[controller.dataArray objectAtIndex:index] tag:_simplePickerTag];
    }
    
    [controller.view removeFromSuperview];
}
- (void)simplePickerViewControllerDidCancelled:(SimplePickerViewController *)controller{
    [controller.view removeFromSuperview];
}

#pragma mark - DatePickerViewControllerDelegate

- (void)datePickerViewControllerDidSelected:(DatePickerViewController *)controller dateStr:(NSString*)dateStr{
    [_confirmVC setDate:dateStr];
    
    [controller.view removeFromSuperview];
}
- (void)datePickerViewControllerDidCancelled:(DatePickerViewController *)controller{
    [controller.view removeFromSuperview];
}

#pragma mark - IBAction

- (IBAction)calcel{
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)goParsing{
    NSArray* arrResult = [ParsingDataMgr parsingResults:_tvSMS.text];
    
    if (arrResult == nil) {
        [ControlUtil showAlertConform:@"카드 리스트 없음"
                                 body:@"저장되어 있지 않은 카드 입니다. 카드를 추가하시겠습니까?"
                                 lBtn:@"확인" rBtn:@"수동입력" delegate:self tag:123];
    }else{
        _confirmVC = [[ConfirmViewController alloc] initWithNibName:@"ConfirmViewController" bundle:nil];
        [_confirmVC set_delegate:self];

        [_tvSMS resignFirstResponder];
        [self.view addSubview:_confirmVC.view];
        [_confirmVC initList:[arrResult objectAtIndex:0]
                   delimiter:[arrResult objectAtIndex:1]
                   checkDate:[arrResult objectAtIndex:2]
                    strPrice:[arrResult objectAtIndex:3]];
    }
}

@end
